<?php
/**
 * pdo_mysql驱动
 *
 * @package if
 * @subpackage module.db.drivers.pdo
 * @since 1.0
 */
class IDbPdoMysql extends IAbstractDbPdo {
	protected $pdoType = "mysql";
	
	protected $dataTypes = array(
		"STRING" => IDb::TYPE_STR,
		"CHAR" => IDb::TYPE_STR,
		"VARCHAR" => IDb::TYPE_STR,
		"TINYBLOB" => IDb::TYPE_STR, 
		"TINYTEXT" => IDb::TYPE_STR, 
		"ENUM" => IDb::TYPE_STR, 
		"SET" => IDb::TYPE_STR, 
		"TEXT" => IDb::TYPE_LOB,
		"LONGTEXT" => IDb::TYPE_LOB, 
		"MEDIUMTEXT" => IDb::TYPE_LOB,
		"IMAGE" => IDb::TYPE_LOB,
		"LONGBLOB" => IDb::TYPE_LOB, 
		"BLOB" => IDb::TYPE_LOB,
		"MEDIUMBLOB" => IDb::TYPE_LOB,
		"BINARY" => IDb::TYPE_LOB,
		"YEAR" => IDb::TYPE_DATE,
		"DATE" => IDb::TYPE_DATE,		
		"TIME" => IDb::TYPE_TIME,
		"DATETIME" => IDb::TYPE_TIMESTAMP,
		"TIMESTAMP" => IDb::TYPE_TIMESTAMP,		
		"INT" => IDb::TYPE_INT, 
		"INTEGER" => IDb::TYPE_INT,
		"BIGINT" => IDb::TYPE_LONG,
		"TINYINT" => IDb::TYPE_INT,
		"MEDIUMINT" => IDb::TYPE_INT,
		"SMALLINT" => IDb::TYPE_INT
	);
	
	protected $defaultDataType = IDb::TYPE_FLOAT;
	
	protected $startQuote = "`";
	protected $endQuote = "`";	
	
	protected $metaTableSQL = "SHOW TABLES FROM %s";
	
	function nConnect() {
		parent::nConnect();
		$charset = $this->config("charset");
		if ($charset) {
			$this->pdo()->exec("SET NAMES '{$charset}'");
		}
	}
	
	/**
	 * Enter description here...
	 * 
	 * @param unknown_type $name
	 * @return array
	 */
	function describeTable($name) {
		$array = $this->fetchAll($this->nativeExec("SHOW COLUMNS FROM `{$name}`"), IDb::FETCH_NUM);
		$fields = array();
		foreach ($array as $row) {
			$fld = new stdClass();
			$fld->name = $row[0];
			$type = $row[1];
			
			// split type into type(length):
			$fld->scale = null;
			if (preg_match("/^(.+)\((\d+),(\d+)/", $type, $query_array)) {
				$fld->type = $query_array[1];
				$fld->max_length = is_numeric($query_array[2]) ? $query_array[2] : -1;
				$fld->scale = is_numeric($query_array[3]) ? $query_array[3] : -1;
			} elseif (preg_match("/^(.+)\((\d+)/", $type, $query_array)) {
				$fld->type = $query_array[1];
				$fld->max_length = is_numeric($query_array[2]) ? $query_array[2] : -1;
			} elseif (preg_match("/^(enum)\((.*)\)$/i", $type, $query_array)) {
				$fld->type = $query_array[1];
				$arr = explode(",",$query_array[2]);
				$fld->enums = $arr;
				$zlen = max(array_map("strlen",$arr)) - 2; // PHP >= 4.0.6
				$fld->max_length = ($zlen > 0) ? $zlen : 1;
			} else {
				$fld->type = $type;
				$fld->max_length = -1;
			}
			$fld->not_null = ($row[2] != 'YES');
			$fld->primary_key = ($row[3] == 'PRI');
			$fld->auto_increment = (strpos($row[5], 'auto_increment') !== false);
			$fld->binary = (strpos($type,'blob') !== false || strpos($type,'binary') !== false);
			$fld->unsigned = (strpos($type,'unsigned') !== false);
			$fld->zerofill = (strpos($type,'zerofill') !== false);

			if (!$fld->binary) {
				$d = $row[4];
				if ($d != '' && $d != 'NULL') {
					$fld->has_default = true;
					$fld->default_value = $d;
				} else {
					$fld->has_default = false;
				}
			}
			
			$fld->data_type = $this->dataType($fld);
			$fields[$fld->name] = get_object_vars($fld);
		}
		return $fields;			
	}	
	
	
	function nLimit($sql, $count, $offset = 0) {
		return $sql . (($offset > 0) ?  " LIMIT {$offset},{$count}" : " LIMIT {$count}");
	}
}


?>